<?php
class M_school_settings extends CI_Model {
	
	private $__table = 'school_settings';
	private $_t = 'online_settings';
	
	public function get_all_school_setting($id = '')
	{
		$query = $this->db->limit(1)->get($this->__table);
		return $query->num_rows() >= 1 ? $query->row() : FALSE;
	}
	/*
		query to update section
		11/12/11/2012
	*/
	public function update_school_setting($input)
	{
		$ss = $this->get_all_school_setting();
		if($ss == FALSE)
		{
			$this->db->insert($this->__table,$input);
		}else{
			$input['updated_at'] = NOW;
			$this->db->set($input)->where('id',$ss->id)->update($this->__table);
		}
		return $this->db->affected_rows() > 0 ? array('status'=>'true') : array('status'=>'false');
	}
	
	public function get_status($select = FALSE)
	{
		if($select !== FALSE)
		{
			$query = $this->db->select('es_id,'.$select.'')
							  ->limit(1)
							  ->get($this->_t);
			return $query->num_rows() >=1 ? $query->row() : FALSE;
		}else{
			$query = $this->db->limit(1)->get($this->_t);
			return $query->num_rows() >=1 ? $query->row() : FALSE;
		}
	}
	
	public function update_online_enrollment($x)
	{
		$query = $this->get_status();
		if(!empty($query))
		{
			if($x == 'e')
			{
				$this->db->set('enable_online_enrollment',1)->where('es_id',$query->es_id)->update($this->_t);
				return $this->db->affected_rows() >=1 ? TRUE : FALSE;
			}else{
				$this->db->set('enable_online_enrollment',0)->where('es_id',$query->es_id)->update($this->_t);
				return $this->db->affected_rows() >=1 ? TRUE : FALSE;
			}
		}else{
			$data['enable_online_enrollment'] = 0;
			$data['enable_student_portal'] = 0;
			$this->db->insert($this->_t,$data);
			return $this->db->affected_rows() >=1 ? TRUE : FALSE;
		}
	}
	
	public function update_student_portal($x)
	{
		$query = $this->get_status();
		if(!empty($query))
		{
			if($x == 'e')
			{
				$this->db->set('enable_student_portal',1)->where('es_id',$query->es_id)->update($this->_t);
				return $this->db->affected_rows() >=1 ? TRUE : FALSE;
			}else{
				$this->db->set('enable_student_portal',0)->where('es_id',$query->es_id)->update($this->_t);
				return $this->db->affected_rows() >=1 ? TRUE : FALSE;
			}
		}else{
			$data['enable_online_enrollment'] = 0;
			$data['enable_student_portal'] = 0;
			$this->db->insert($this->_t,$data);
			return $this->db->affected_rows() >=1 ? TRUE : FALSE;
		}
	}
	
	function update_offline_content($data)
	{
		if($this->get_status() == FALSE)
		{
			$data['enable_online_enrollment'] = 0;
			$data['enable_student_portal'] = 0;
			$this->db->insert($this->_t,$data);
		}else
		{
			$this->db->set($data)->update($this->_t);
		}
		return $this->db->affected_rows() >=1 ? TRUE : FALSE;
	}
	
	public function update_success_message($message = NULL,$id)
	{
		$db_msg = $this->get_custom_message($id);
		

		if($db_msg)
		{
			$update['message'] = htmlentities(trim($message),ENT_QUOTES); 
			
			$this->db->set($update)
					 ->where('id',$db_msg->id)
					 ->update('system_custom_message');
		}else{

			$insert['message'] = htmlentities(trim($message),ENT_QUOTES);
			$this->db->insert('system_custom_message',$insert);
		
		}
		
		// pd($this->db->last_query());
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	
	public function get_custom_message($id)
	{
		$sql = 'SELECT id,type,message 
				FROM system_custom_message scm
				WHERE id = ?
				LIMIT 1';
				
		$q = $this->db->query($sql,array($id));
		
		return $q->num_rows() >= 1 ? $q->row() : FALSE;
	}
	
	public function get_all_custom_message()
	{
		$q = $this->db->get('system_custom_message');
		
		return $q->num_rows() >=1 ? $q->result() : FALSE;
	}
}
?>